﻿using System.IO;
using EnvDTE;
using Microsoft.VisualStudio.SharePoint;
using SP2010DevExtensions.Helper;

namespace SP2010DevExtensions.ItemExtensions
{
    public class Constants
    {
        public const string PhysicalFileGuidID = "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}";
        public const string PhysicalFolderGuidId = "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}";
        public const string VirtualFolderGuidId = "{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}";
        public const string SubProjectGuidId = "{EA6618E8-6E24-4528-94BE-6889FE16485C}";
    }
    public class MenuCommands
    {
        
        public static void CopyTo(IMappedFolder mappedFolder)
        {


            var projectService = mappedFolder.Project.ProjectService;

            var localPath = projectService.SharePointInstallPath;
            switch (mappedFolder.FolderType)
            {
                case MappedFolderType.ControlTemplates:

                    localPath = Path.Combine(localPath, @"template\controltemplates");
                    break;
                case MappedFolderType.Images:
                    localPath = Path.Combine(projectService.SharePointInstallPath, @"template\images");
                    break;
                case MappedFolderType.Layouts:
                    localPath = Path.Combine(projectService.SharePointInstallPath, @"template\layouts");
                    break;
                case MappedFolderType.Template:
                    localPath = Path.Combine(localPath, @"Template");
                    break;
            }
            var projectItem = SharePointMappingFolderHelper.ConvertProjectItem(mappedFolder);

            CopyToFolder(localPath, projectItem);

        }

        private static void CopyToFolder(string rootPath, ProjectItem currentFolder)
        {
            
            foreach (ProjectItem item in currentFolder.ProjectItems)
            {
                if (item.Kind == Constants.PhysicalFolderGuidId)
                {
                    CopyToFolder(Path.Combine(rootPath, item.Name), item);
                }
                else
                {
                    if (!Directory.Exists(rootPath))
                    {
                        Directory.CreateDirectory(rootPath);
                    }
                    File.Copy(item.FileNames[1], Path.Combine(rootPath,item.Name));
                }
            }
        }
        
    }
}
